package samsung.sds.roleManagement.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import samsung.sds.roleManagement.model.RoleManagementEntity;
import samsung.sds.roleManagement.model.RoleToResEntity;
import samsung.sds.roleManagement.model.UserToRoleEntity;
import samsung.sds.roleManagement.service.RoleManagementService;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.CommonTools;
import samsung.sds.system.utils.Constants;
import samsung.sds.userManagement.model.UserEntity;

/**
 * 
 * @author fei3.liu
 * @createDate 2016-01-26
 * 
 */

@Controller
@RequestMapping("/roleManagement")
public class RoleManagementController {

	@Autowired
	private RoleManagementService roleManagementServiceImpl;

	private static final String url = "/roleManagement";

	/**
	 * Jump role list
	 * 跳转至roleList页面
	 */
	@RequestMapping(value = "/roleList",method = RequestMethod.POST)
	public String roleList(HttpServletRequest request,ModelMap modelMap) {
		String pageType = request.getParameter("pageType");
		modelMap.addAttribute("pageType", pageType);
		return url + "/roleList";
	}

	/**
	 * To produce a list of roles
	 * 获取角色列表，如果有查询条件时按照条件查询
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/listPageData",method = RequestMethod.POST)
	@ResponseBody
	public PageData<RoleManagementEntity> getList(HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		return roleManagementServiceImpl.queryListPage(param);

	}
	
	/**
	 * Jump new or modify the page before the judge whether the role object.
	 * 在跳转新建或修改角色页面之前判断是否需要role对象。
	 * 
	 * @param role_name
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/roleDetails", method = RequestMethod.POST)
	public String roleDetails(String role_name,ModelMap modelMap) {
		RoleManagementEntity role = roleManagementServiceImpl.findEntityById(role_name);
		modelMap.addAttribute("role", role);
		return url + "/addOrUpdateRole";
	}

	/**
	 * New or modified roles
	 * 新建或修改角色
	 * 
	 * @param optype
	 * @param role
	 */
	@RequestMapping(value = "/addOrUpdateRole",method = RequestMethod.POST)
	@ResponseBody
	public String addOrUpdateRole(String optype,RoleManagementEntity role) {
		if (role == null || optype == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}

		// 执行新增角色
		if (optype.equalsIgnoreCase("create")) {
			int rlt = roleManagementServiceImpl.insertEntity(role);
			if (rlt == 1) {
				return Constants.SUCCESS_CODE;
			} else {
				return Constants.FAILURE_CODE;
			}

		// 执行修改角色
		} else if (optype.equalsIgnoreCase("update")) {

			int rlt = roleManagementServiceImpl.updateEntity(role);
			if (rlt == 1) {
				return Constants.SUCCESS_CODE;
			} else {
				return Constants.FAILURE_CODE;
			}
			// optype标签未知状态
		} else {
			return Constants.ERRMSG_PARAM_ERROR;
		}
	}
	
	/**
	 * The uniqueness of inspection role 
	 * 检测角色名的唯一性
	 * 
	 * @param role_name
	 * @param optype
	 */
	@RequestMapping(value = "/checkRoleName",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkRoleName(String role_name,String optype) {
		if (roleManagementServiceImpl.findEntityById(role_name) != null && "create".equals(optype)) {
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * Delete role
	 * 删除角色
	 * 
	 * @param id
	 */
	@RequestMapping(value = "/deleteRole",method = RequestMethod.POST)
	@ResponseBody
	public String deleteRole(@RequestBody String id) {
		// 检查id是否为空，如果为空，返回错误画面
		if (id == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		List<UserToRoleEntity> userToRoleList = roleManagementServiceImpl.queryUserByRoleName(id);
		List<RoleToResEntity> roleToResList = roleManagementServiceImpl.queryResByRoleName(id);
		if (userToRoleList.isEmpty() && roleToResList.isEmpty()) {
			// 执行删除
			int rlt = roleManagementServiceImpl.deleteEntityById(id);
			if (rlt == 1) {
				return Constants.SUCCESS_CODE;
			} else {
				return Constants.FAILURE_CODE;
			}
			//如果角色同时被用户和资源引用，返回“角色名称被角色和资源同时引用，删除失败”错误提示。
		} else if(!userToRoleList.isEmpty() && !roleToResList.isEmpty()){
			return "userAndResUsing";
			//如果角色被用户引用，返回“角色名称被用户引用，删除失败”错误提示。
		}else if(!userToRoleList.isEmpty()){
			return "userUsing";
			//如果角色被资源引用，返回“角色名称被资源引用，删除失败”错误提示。
		}else if(!roleToResList.isEmpty()){
			return "resUsing";
		}else{
			//返回删除失败。
			return Constants.FAILURE_CODE;
		}
	}

	/**
	 * Remove the user associated with the role of 
	 * 解除角色与用户之间的关联，即从SYS_USER_TO_ROLR删除数据
	 * 
	 * @param unameAndRname
	 */
	@RequestMapping(value = "/deleteUserToRoleData",method = RequestMethod.POST)
	@ResponseBody
	public String deleteUserToRoleData(@RequestBody String unameAndRname) {
		//判断从前台页面传过来的值是否为空，为空时返回错误数据
		if (unameAndRname == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		String rlt = roleManagementServiceImpl.deleteUserToRole(unameAndRname);
		if(rlt.equals("success")){
			return Constants.SUCCESS_CODE;
		}else{
			return Constants.FAILURE_CODE;
		}
	}

	/**
	 * Add users to roles 
	 * 给角色添加用户
	 * 
	 * @param userToRoleData
	 */
	@RequestMapping(value = "/insertRoleUserToRoleData",method = RequestMethod.POST)
	@ResponseBody
	public String insertRole(@RequestBody String userToRoleData) {
		//判断从前台页面传过来的值是否为空，为空时返回错误数据
		if (userToRoleData == null) {
			return Constants.ERRMSG_PARAM_ERROR;
		}
		String rlt = roleManagementServiceImpl.insertUserToRole(userToRoleData);
		if(rlt.equals("success")){
			return Constants.SUCCESS_CODE;
		}else{
			return Constants.FAILURE_CODE;
		}
	}

	/**
	 * Jump viewUserPopup page
	 * 跳转viewUserPopup页面。
	 * 
	 */
	@RequestMapping(value = "/viewRolePopup_sitemesh_popup",method = RequestMethod.GET)
	public String viewUserPopup() {
		return url + "/viewUserPopup";
	}

	/**
	 * Jump addUserPopup page
	 * 跳转addUserPopup页面
	 */
	@RequestMapping(value = "/addUserPopup_sitemesh_popup",method = RequestMethod.GET)
	public String addUserPopup() {
		return url + "/addUserPopup";
	}

	/**
	 * Popup page according to the role_name query to the user list.
	 * 查询拥有此角色的用户
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/queryUserListByRoleName",method = RequestMethod.POST)
	@ResponseBody
	public PageData<UserEntity> queryUserListByRoleName(HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		PageData<UserEntity> userRoleEntityList = 
				roleManagementServiceImpl.queryUserListByRoleNameListPage(param);
		return userRoleEntityList;
	}
	
	/**
	 * Users who do not have this role according to the user name
	 * 查询不拥有此角色的用户
	 * 
	 * @param request
	 */
	@RequestMapping(value = "/queryUserListExceptRoleName",method = RequestMethod.POST)
	@ResponseBody
	public PageData<UserEntity> queryUserListExcepyRoleName(HttpServletRequest request) {
		QueryParam param = CommonTools.getQueryParam(request);
		PageData<UserEntity> userRoleEntityList = 
				roleManagementServiceImpl.queryUserListExceptRoleNameListPage(param);
		return userRoleEntityList;
	}
}
